package br.usp.icmc.biomac.client.view.impl;

import java.util.Comparator;
import java.util.Date;
import java.util.List;

import br.usp.icmc.biomac.client.view.ROIAnnotationCellTable;
import br.usp.icmc.biomac.shared.ROI;
import br.usp.icmc.biomac.shared.Slider;

import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SelectionModel;
import com.google.gwt.view.client.SingleSelectionModel;

public class ROIAnnotationCellTableImpl extends Composite implements ROIAnnotationCellTable {

	private static ROIAnnotationCellTableImplUiBinder uiBinder = GWT
			.create(ROIAnnotationCellTableImplUiBinder.class);
	@UiField(provided = true)
	CellTable<ROI> cellTable = new CellTable<ROI>();
	@UiField
	SimplePager pager1;
	@UiField
	SimplePager pager2;



	ListDataProvider<ROI> dataProvider;// = new ListDataProvider<ROI>();
	
	Presenter presenter;

	interface ROIAnnotationCellTableImplUiBinder extends
			UiBinder<Widget, ROIAnnotationCellTableImpl> {
	}

	public ROIAnnotationCellTableImpl(ListDataProvider<ROI> dataProvider) {
		this.dataProvider = dataProvider;
		initWidget(uiBinder.createAndBindUi(this));

		// cellTable.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
		cellTable.setWidth("100%", true);

		// Connect the table to the data provider.
		this.dataProvider.addDataDisplay(cellTable);

		// Create a Pager to control the table.
		pager1.setDisplay(cellTable);
		pager2.setDisplay(cellTable);
		
	

		// Attach a column sort handler to the ListDataProvider to sort the
		// list.
		ListHandler<ROI> sortHandler = new ListHandler<ROI>(
				dataProvider.getList());
		cellTable.addColumnSortHandler(sortHandler);

		// Add a selection model so we can select cells.
		final SingleSelectionModel<ROI> selectionModel = new SingleSelectionModel<ROI>();
		selectionModel
				.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
					@Override
					public void onSelectionChange(SelectionChangeEvent event) {
						ROI selected = selectionModel.getSelectedObject();
						if (selected != null) {
							// setThubmanais(selected.getSeries());
						}
					}
				});

		cellTable.setSelectionModel(selectionModel);

		// Initialize the cell table
		initTableColumns(selectionModel, sortHandler);
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void setDataResult(List<ROI> data) {

		// Add the data to the data provider, which automatically pushes it to
		// the widget.
		List<ROI> list = dataProvider.getList();
		list.clear();
		for (ROI roi : data) {
			list.add(roi);
		}
		
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void setDataProvider(ListDataProvider<ROI> dataProvider) {
		this.dataProvider = dataProvider;	
	}
	
	public void initTableColumns(final SelectionModel<ROI> selectionModel,
			ListHandler<ROI> sortHandler) {

		// Create dateTime column.
		Column<ROI, Date> dateTime = new Column<ROI, Date>(
				new DateCell()) {
			@Override
			public Date getValue(ROI object) {
				return new Date();//object.getDateAcquired();
			}
		};
		// Make the examType column sortable.
		dateTime.setSortable(true);
		sortHandler.setComparator(dateTime, new Comparator<ROI>() {
			@Override
			public int compare(ROI o1, ROI o2) {
				return o1.getDateTime().compareTo(o2.getDateTime());
			}
		});
		// add dateAcquired in cellTable
		cellTable.addColumn(dateTime, "Date and Time");
		cellTable.setColumnWidth(dateTime, 180, Unit.PX);
		
		// Create Annotation Name column.
		Column<ROI, String> labelROI = new Column<ROI, String>(new ClickableTextCell()) {	
			@Override
			public String getValue(ROI object) {
				return object.getLabel();
			}
		};
		labelROI.setFieldUpdater(new FieldUpdater<ROI, String>() {
			@Override
			public void update(int index, ROI object, String value) {
				//Window.alert(object.getLabel());
				presenter.goToAnnotationTab(object);
			}
		});
		
		// Make the Annotation Name column sortable.
		labelROI.setSortable(true);
		sortHandler.setComparator(labelROI, new Comparator<ROI>() {
			@Override
			public int compare(ROI arg0, ROI arg1) {
				return arg0.getLabel().compareTo(arg1.getLabel());
			}
		});
		// add Annotation Name in cellTable
		cellTable.addColumn(labelROI, "Annotation Name");
	//	cellTable.setColumnWidth(labelROI, 150, Unit.PX);

		// Create patientID column.
		Column<ROI, String> patientID = new Column<ROI, String>(new TextCell()) {
			@Override
			public String getValue(ROI object) {
				return object.getLabel();
			}
		};
		// Make the patientID column sortable.
		patientID.setSortable(true);
		sortHandler.setComparator(patientID, new Comparator<ROI>() {
			@Override
			public int compare(ROI arg0, ROI arg1) {
				return arg0.getLabel().compareTo(arg1.getLabel());
			}
		});
		// add patientID in cellTable
		cellTable.addColumn(patientID, "Pattient ID");
		cellTable.setColumnWidth(patientID, 80, Unit.PX);

		// Create patientName column.
		Column<ROI, String> patientName = new Column<ROI, String>(new TextCell()) {
			@Override
			public String getValue(ROI object) {
				return object.getLabel();
			}
		};
		// Make the patientName column sortable.
		patientName.setSortable(true);
		sortHandler.setComparator(patientName, new Comparator<ROI>() {
			@Override
			public int compare(ROI arg0, ROI arg1) {
				return arg0.getLabel().compareTo(arg1.getLabel());
			}
		});
		// add patientName in cellTable
		cellTable.addColumn(patientName, "Patient Name");
		cellTable.setColumnWidth(patientName, 80, Unit.PX);

	}
	
	@Override
	public void redraw(){
		cellTable.redraw();
	}
}
